/**
 * $Id: dispatcher.c,v 1.3 2004/11/09 17:50:26 andrei Exp $
 *
 * dispatcher module -- stateless load balancing
 *
 * Copyright (C) 2004-2006 FhG Fokus
 *
 * This file is part of ser, a free SIP server.
 *
 * ser is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version
 *
 * For a license to use the ser software under conditions
 * other than those described here, or to purchase support for this
 * software, please contact iptel.org by e-mail at the following addresses:
 *    info@iptel.org
 *
 * ser is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/**
 * History
 * -------
 * 2004-07-31  first version, by dcm
 * 
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <openssl/hmac.h>

#include "../../sr_module.h"
#include "../../dprint.h"
#include "../../error.h"
#include "../../ut.h"
#include "../../mem/mem.h"

//#include "dispatch.h"

MODULE_VERSION

/** parameters */
//char *dslistfile = CFG_DIR"dispatcher.list";
//int  force_dst = 0;

/** module functions */
static int my_function(struct sip_msg*, char *str1,char *str2);
static int mod_init(void);

//static int w_ds_select_dst(struct sip_msg*, char*, char*);
//my functions!!!

// //int use_hmac(void);
char * test(char *m);
unsigned char * create_hmac_3(unsigned char *message, unsigned char *key);

static void  hex_to_str_3(unsigned char *hmac, char *test)
 {
         int counter;
         char *hex_value;
    	 LOG(L_ERR,"HEX TO STR 3");
         hex_value=( char *)pkg_malloc(sizeof(char)*2);
         memset(hex_value,0,sizeof(char)*2);
         for(counter=0;counter<16;counter++){
		LOG(L_ERR,"%.2X",hmac[counter]);
		 sprintf(hex_value,"%.2X",hmac[counter]);
		 LOG(L_ERR,"hex_valuet:is %s",hex_value);
                 strcat(test,hex_value);
		 LOG(L_ERR,"test:is %s",test);
                 memset(hex_value,0,sizeof(char)*2);
         }
	pkg_free(hex_value);
	//LOG(L_ERR,"!!!!print the whole string:%s\n",test);
 }

 

//static int ds_fixup(void** param, int param_no);

static cmd_export_t cmds[]={
	{"s_my_test", my_function, 0, 0, REQUEST_ROUTE},
	{0,0,0,0,0}
};


static param_export_t params[]={
	{0,0,0}
};


/** module exports */
struct module_exports exports= {
	"mytest",
	cmds,
	params,
	
	mod_init,   /* module initialization function */
	0,
	0,
	0,
	0  /* per-child init function */
};

/**
 * init module function
 */
static int mod_init(void)
{
	//DBG("DISPATCHER: initializing ...\n");
	//LOG(L_NOTICE,"!!!THIS IS MY TEST!!! initialization");
	//fprintf(stderr, "print - initializing\n");
	
	return 0;
}



/**
 * My Fucntion
 */
static int my_function(struct sip_msg *msg, char *str1,char *str2)
{

	int irc;
	int counter;
	char *initial_message;
	char *str_hex;
 	char *sip_msg;
	unsigned char *key;
	unsigned char *result_hmac;
	unsigned char *result_hex;
	char *hex_value;
    //	 LOG(L_ERR,"HEX TO STR 3");
         hex_value=( char *)pkg_malloc(sizeof(char)*2);
         memset(hex_value,0,sizeof(char)*2);

	if ( !msg->integrity_auth ) {
		//LOG(L_ERR,"MY MODULE!!!my function!!!the integrity header is not parsed!!!\n");
		if(parse_headers( msg , HDR_INTEGRITYAUTH, 0 )==-1 ){
			return -1;
		}
		if(!msg->integrity_auth){
			LOG(L_ERR,"this is the best mymodule:I am NOT in a good way1111!!!!!???\n");
			return -1;
		}
		
	}

	//auto einai to value apo to arxiko minima...

	str_hex=(char *)pkg_malloc(sizeof(char)*msg->integrity_auth->body.len+1);
	memset(str_hex,0,sizeof(char)*msg->integrity_auth->body.len+1);
	memcpy(str_hex,msg->integrity_auth->body.s,msg->integrity_auth->body.len);
	
//	LOG(L_ERR,"the initiail str_hex is%s",str_hex);	

	initial_message=test(msg->orig);
	sip_msg=(char *)pkg_malloc(sizeof(char)*msg->len);
        memset(sip_msg,0,sizeof(char)*msg->len);

	bcopy(initial_message,sip_msg,strlen(initial_message)-1);
	
//	LOG(L_ERR,"the sip_msg%s",sip_msg);

	key=(unsigned char *)pkg_malloc(sizeof(char)*16);
        memset(key,0,sizeof(char)*16);
        strcpy(key,"Jefe");

 	

        result_hex=( char *)pkg_malloc(sizeof(char)*33);
	memset(result_hex,0,sizeof(char)*33);

	
	result_hmac=create_hmac_3(sip_msg,key);

	//for(irc=0;irc<16;irc++){
	//	LOG(L_ERR,"%.2X",result_hmac[irc]);
	//}


 	

         for(counter=0;counter<16;counter++)
	{
		
		 sprintf(hex_value,"%.2X",result_hmac[counter]);
		 strcat(result_hex,hex_value);
		 memset(hex_value,0,sizeof(char)*2);
         }
	//memset(result_hex,0,33);
	//hex_to_str_3(result_hmac,result_hex);
//	LOG(L_ERR,"%s",result_hex);
	//LOG(L_ERR,"%s",result_hex);

	if(memcmp(result_hex,str_hex,msg->integrity_auth->body.len)!=0){
		LOG(L_ERR,"ta string den einai ta idia");	
		pkg_free(result_hex);
		pkg_free(sip_msg);
		pkg_free(str_hex);
		pkg_free(key);
		pkg_free(result_hmac);
		pkg_free(initial_message);//auto to anoija teleutaio...
		return -1;
	}

	pkg_free(result_hex);
	pkg_free(sip_msg);
	pkg_free(str_hex);
	pkg_free(key);
	pkg_free(result_hmac);
	pkg_free(initial_message);//auto to anoija teleutaio...*/
	pkg_free(hex_value);
	return 1;
}

char * 
test(char *buffer)
{
	int irc;
	char *part1;
	char *part2;
	char *tmp;
	char *message;
	FILE *fp;
	char* str2 = "Inte";
	char* str3 = "Max";
	int length= strlen(buffer);
	char *result1;
	//char *result2;

	message=(char *)pkg_malloc(sizeof(char)*length);
	memset(message,0,sizeof(char)*length);
	
	result1=(char *)pkg_malloc(sizeof(char)*length);
	memset(result1,0,sizeof(char)*length);
	//strncpy	
	
	tmp = strstr( buffer, str2 );
	strcat(result1,tmp);
	//bcopy(buffer,message,length-strlen(result1));
	memcpy(message,buffer,length-strlen(result1));
	
		
  	if( tmp == NULL ) 
		LOG(L_ERR, "Could not find the inte header");
  	else{
		//LOG(L_ERR, "Found a substring: '%s'\n", result );
		part2=memchr(result1,'\n',strlen(buffer));
	//	LOG(L_ERR, "Found a substring: %s", part2 );
	}
	strcat(message,part2+1);
	memset(result1,0,sizeof(char)*length);
	tmp = strstr(message,str3);
	strcat(result1,tmp);
	
	pkg_free(message);

	return (result1);
	

}

/**
 * destroy function
 */

/**
 * test function
 */
/*
int use_hmac()
{
        //definitions
        unsigned char *result;
        unsigned char *key;
        int key_length;
        unsigned char *sip_msg;
        unsigned char *hmac;
	unsigned char *result_hex;
        int hmac_length;
         //initialization
        hmac_length=0;
        result=(char *)malloc(sizeof(char)*EVP_MAX_MD_SIZE);
        memset(result,0,sizeof(char)*EVP_MAX_MD_SIZE);

        result_hex=(char *)malloc(sizeof(char)*EVP_MAX_MD_SIZE);
        memset(result_hex,0,sizeof(char)*EVP_MAX_MD_SIZE);

        //
        hmac=(char *)malloc(sizeof(char)*EVP_MAX_MD_SIZE);
        memset(hmac,0,sizeof(char)*EVP_MAX_MD_SIZE);
        //
        key=(unsigned char *)malloc(sizeof(char)*16);
        memset(key,0,sizeof(char)*16);
        strcpy(key,"Jefe");
        key_length=strlen(key);
        //
        sip_msg=(unsigned char *)malloc(sizeof(char)*1000);
        memset(sip_msg,0,sizeof(char)*1000);
       // strcpy(sip_msg,"what do ya want for nothing?");

        memset(result,0,sizeof(char)*EVP_MAX_MD_SIZE);
        result=create_hmac(sip_msg,key);
        hex_to_str(result,result_hex);
	
       return 0;
}*/

unsigned char *
create_hmac_3(unsigned char *message, unsigned char *key)
 {
         int key_length;
         int hmac_length;
         int msg_length;
         unsigned char *hmac;
         unsigned char *result;

         //initialization

         result=(char *)pkg_malloc(sizeof(char)*EVP_MAX_MD_SIZE);
         memset(result,0,sizeof(char)*EVP_MAX_MD_SIZE);
         hmac=(char *)pkg_malloc(sizeof(char)*EVP_MAX_MD_SIZE);
         memset(hmac,0,sizeof(char)*EVP_MAX_MD_SIZE);

         hmac_length=0;
         key_length=strlen(key);
         msg_length=strlen(message);

         result=HMAC(EVP_md5(),key,key_length,message,msg_length,hmac,&hmac_length);
	 pkg_free(hmac);
         return result;
 }
 

